import { reqGoodsInfo, reqAddOrUpdateShopCar } from '@/api'
import { getUUID } from '@/utils/uuid_token'
const state = {
  goodInfo: {},
  uuid_token: getUUID()
}

//mutations 修改state的唯一手段  state.categoryList是上方state里的空数组，后面的赋值是从actions里收到的参数result.data
const mutations = {
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }

}

//actions 可以书写的业务
const actions = {
  async getGoodInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code == 200) {
      commit('GETGOODINFO', result.data)
    }

  },
  async addOrUpdateShopCar({ commit }, { skuId, skuNum }) {
    let result = await reqAddOrUpdateShopCar(skuId, skuNum)
    if (result.code == 200) {
      return '成功'
    } else {
      return Promise.reject(new Error('faile'))
    }
  }
}


//理解为计算属性用于简化仓库数 ，让组件读取数据更快
const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}